package com.service.infocenter.imp;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import com.mapper.infocenter.RoleMapper;
import com.mapper.infocenter.UserMapper;
import com.mapper.infocenter.UserRegisterMapper;
import com.mapper.infocenter.UserRoleMapper;
import com.po.PermissionInfo;
import com.po.UserInfo;
import com.po.UserRegisterInfo;
import com.po.UserRoleInfo;
import com.service.infocenter.UserService;
import com.util.MD5Util;

@Service
public class UserServiceImp implements UserService{
	@Resource(name="userMapper")
	private UserMapper userMapper; 	
	@Resource(name="userRegisterMapper")
	private UserRegisterMapper userRegisterMapper; 
	@Resource(name="roleMapper")
	public RoleMapper roleMapper;
	@Resource(name="userRoleMapper")
	public UserRoleMapper userRoleMapper;
	
	@Override
	public List<UserInfo> getUserList(UserInfo user) {
		List<UserInfo> list=userMapper.getUserList(user);
		return list;
	}
	@Override
	public Integer getUserCount(UserInfo user) {
		Integer i=userMapper.getUserCount(user);
		return i;
	}
	@Override
	public int deleteUser(String userid) {
		int i=userMapper.deleteUser(userid);
		return i;
	}

	@Override
	public int deleteUserList(String[] userid) {
		int i=userMapper.deleteUserList(userid);
		return i;
	}
	@Override
	public int addUser(UserInfo user) {
		user.setUser_password(MD5Util.getMD5(user.getUser_password()));
		//查找角色类型
		String type=roleMapper.getRoleID("普通用户");
		UserRegisterInfo userRegisterInfo=new UserRegisterInfo();
		userRegisterInfo.setUser_id(user.getUser_id());
		userRegisterInfo.setUser_name(user.getUser_name());
		userRegisterInfo.setUser_password(user.getUser_password());		
		userRegisterInfo.setUser_type(type);
		//注释：此处添加用户有效
		userRegisterInfo.setUser_validate("1");
		int i=userRegisterMapper.addUserRegister(userRegisterInfo);
		UserRoleInfo userRoleInfo=new UserRoleInfo();
		userRoleInfo.setUser_id(user.getUser_id());
		userRoleInfo.setRole_id(type);
		int j=0,m=0;
		if(i>0){
			 j=userMapper.addUser(user);
			 m=userRoleMapper.addUserRole(userRoleInfo);
		}
		if(i==1&j==1&m==1)
			return 1;
		else return 0;
	}
	@Override
	public int updateUser(UserInfo user) {
		int i=userMapper.updateUser(user);
		return i;
	}
	@Override
	public int deleteUserT(String user_id) {
		int i=userMapper.deleteUserT(user_id);
		return i;
	}
	@Override
	public UserInfo getUser(String user_id) {
		UserInfo userInfo=userMapper.getUser(user_id);
		return userInfo;
	}
	@Override
	public boolean getUserLogin(String user_id,String user_password,HttpServletRequest request) {
		// TODO Auto-generated method stub
		UserInfo userInfo =userMapper.getUserLogin(user_id);
		if(userInfo!=null){
			if(userInfo.getUser_password().equals(MD5Util.getMD5(user_password)))
			{
				HttpSession session = request.getSession();
				session.setAttribute("user_id", user_id);
				session.setAttribute("role_id", userInfo.getUser_style_id());
				session.setAttribute("role_name",userInfo.getUser_style());
				return true;
			}
		}
		return false;
	}
	@Override
	public List<PermissionInfo> getUserPermission(String userid) {
		
		List<PermissionInfo> permissionlist = userMapper.getUserPermission(userid);
		return permissionlist;
	}
	@Override
	public int updateUserImage(String pictureurl, String userid) {
		int result = userMapper.updateUserImage(pictureurl, userid);
		return result;
	}
	@Override
	public int addLoginhistory(String userid) {
		// TODO Auto-generated method stub
		int i = userMapper.addLoginhistory(userid);
		return i;
	}
	@Override
	public int updateUserLastTime(String userid) {
	    int i=userMapper.updateUserLastTime(userid);
	    return i;
	}
}
